var runCount = 0;
(function($){
	$(document)
	.ready(function(){
		if( $('#media-upload').size() == 0 ) return false;
		
		var mediaItems = $('#media-items .media-item');
		
		mediaItems
		.each(function(){
			var el = $(this);
			var input = $('<input type="checkbox" name="toAdd" style="float:right;margin-top:12px;"/>')
			.attr('title','Check to add multiple to editor');

			var span = $('<span style="display:block;float:right;line-height:36px;margin-right:20px;">')
			.append(input);
			
			el.find('a.toggle:eq(1)').after(span);
		});
		

		var pageurl = $('#library-form').attr('action');
		var _wpnonce = $('#_wpnonce').val();
		var _wp_http_referer = $('input[name="_wp_http_referer"]').val();
		var post_id = $('#post_id').val();
		var runCount = 0;

		
		$('<input type="submit" value="Add all" name="addAll" class="button"/>')
		.appendTo('.ml-submit')
		.click(function(){
			var inputs = $('input[name=toAdd]:checked');
			
			runCount = runCount + inputs.size();
			
			inputs.each(function(){
				var el = $(this);
				var id = el.parent().parent().attr('id').split('-').pop();
				
				var post_title 		= $('input[name="attachments['+id+'][post_title]"]').val();
				var post_excerpt 	= $('input[name="attachments['+id+'][post_excerpt]"]').val();
				var post_content 	= $('input[name="attachments['+id+'][post_content]"]').val();
				var url 			= $('input[name="attachments['+id+'][url]"]').val();
				var align			= $('input[name="attachments['+id+'][align]"]:checked').val();
				var image_size		= $('input[name="attachments['+id+'][image-size]"]:checked').val();
				var send		= $('input[name="send['+id+']"]').val();
				
				if(typeof post_title == 'undefined')
					post_title = '';
					
				if(typeof post_excerpt == 'undefined')
					post_excerpt = '';
					
				if(typeof post_content == 'undefined')
					post_content = '';
					
				if(typeof url == 'undefined')
					url = '';
					
				if(typeof align == 'undefined')
					align = '';
					
				if(typeof image_size == 'undefined')
					image_size = '';
				
				var data = 'attachments['+id+'][post_title]='+post_title;
				data += '&attachments['+id+'][post_excerpt]='+post_excerpt;
				data += '&attachments['+id+'][post_content]='+post_content;
				data += '&attachments['+id+'][url]='+url;
				data += '&attachments['+id+'][align]='+align;
				data += '&attachments['+id+'][image-size]='+image_size;
				data += '&send['+id+']='+send;
				data += '&_wpnonce='+_wpnonce;
				data += '&_wp_http_referer='+_wp_http_referer;
				data += '&post_id='+post_id;

				$.ajax({
					url:pageurl,
					dataType : 'html',
					type: 'POST',
					data:data,/*
					dataFilter:function(data){
						var myregexp = /\n\r/;
						console.log( data );
						data = data.replace(myregexpg, "");
						console.log( data );
						return data;
					},*/
					success:function(r){
						result = r.replace(/win\.send_to_editor/g, "px_send_to_editor");
						$('head').append( result );
					}
				});

			});

			return false;
		});
	});
	

})(jQuery);

try{
	function px_send_to_editor(h) {
		runCount--;
		var win = window.dialogArguments || opener || parent || top; 
		if ( typeof win.tinyMCE != 'undefined' && ( ed = win.tinyMCE.activeEditor ) && !ed.isHidden() ) {
			ed.focus();
			if (win.tinymce.isIE)
				ed.selection.moveToBookmark(win.tinymce.EditorManager.activeEditor.windowManager.bookmark);
	
			if ( h.indexOf('[caption') != -1 )
				h = ed.plugins.wpeditimage._do_shcode(h);
	
			ed.execCommand('mceInsertContent', false, h);
		} else if ( win.jQuery.isFunction( 'edInsertContent' ) ) {
			win.edInsertContent(win.edCanvas, h);
		} else {
			win.jQuery( win.edCanvas ).val( win.jQuery( win.edCanvas ).val() + h );
		}
		if(runCount <= 0){
			win.tb_remove();
		}
	}
}
catch(e){console.log(e);}